/**
 * 
 */
package fr.cephb.servlet;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;



/**
 * Abstract Implementation of {@link AbstractExportFilter}
 * for a filter exporting a table
 *
 */
public abstract class AbstractTabularExportFilter extends AbstractExportFilter
	{
	/** loop over the response  as a DOM an tries to find a tag <code>table</code> . If found, calls <b>parseTable</b> */
	@Override
	protected void export(File content, HttpServletRequest req,
			HttpServletResponse res) throws IOException, ServletException {
		XMLEventReader reader=null;
		try {
			XMLInputFactory f= XMLInputFactory.newInstance();
			f.setProperty(XMLInputFactory.IS_COALESCING, true);
			f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
			f.setProperty(XMLInputFactory.IS_VALIDATING, false);
			f.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
			
			reader=f.createXMLEventReader(new FileReader(content));
			
			while(reader.hasNext())
				{
				XMLEvent evt= reader.nextEvent();
				if(evt.isStartElement())
					{
					StartElement e=evt.asStartElement();
					QName name= e.getName();
					if(name.getLocalPart().equals("table"))
						{
						parseTable(reader,e,req,res);
						break;
						}
					}
				}
			reader.close();
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		finally
			{
			if(reader!=null) try{ reader.close(); } catch(Throwable err) { }
			}
		}

	/** parses the table found */
	public void parseTable(XMLEventReader reader,StartElement table,HttpServletRequest req,HttpServletResponse res) throws XMLStreamException,IOException,ServletException
		{
		int nRow=-1;
		int nCol=-1;
		StringBuilder content= null;
		startTable(req,res);
		while(reader.hasNext())
			{
			XMLEvent evt= reader.nextEvent();
			if(evt.isStartElement())
				{
				StartElement e=evt.asStartElement();
				String name= e.getName().getLocalPart();
				if(name.equals("tr"))
					{
					++nRow;
					startRow(req,res,nRow);
					}
				else if(name.equals("th") || name.equals("td"))
					{
					nCol++;
					startCell(req, res, e, nRow	, nCol);
					content=new StringBuilder();
					}
				}
			else if(evt.isEndElement())
				{
				EndElement e=evt.asEndElement();
				String name= e.getName().getLocalPart();
				if(name.equals("tr"))
					{
					endRow(req,res,nRow);
					nCol=-1;
					}
				else if(name.equals("th") || name.equals("td"))
					{
					endCell(req,res,e,nRow,nCol,content==null?"":content.toString());
					content=null;
					}
				else if(name.equals("table"))
					{
					break;
					}
				}
			else if(evt.isCharacters() && content!=null)
				{
				content.append(evt.asCharacters().getData());
				}
			else if(evt.isEntityReference() && content!=null)
				{
				content.append(evt.asCharacters());
				}
			}
		endTable(req,res);
		}

	public void startTable(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
		{
		
		}
	
	public void endTable(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
		{
		
		}
	
	public void startRow(HttpServletRequest req,HttpServletResponse res,int nLine) throws IOException,ServletException
		{
		
		}
	
	public void endRow(HttpServletRequest req,HttpServletResponse res,int nLine) throws IOException,ServletException
		{
	
		}
	
	
	
	public void startCell(HttpServletRequest req,HttpServletResponse res,StartElement e,int row,int col) throws IOException,ServletException
		{
		
		}
	
	public void endCell(HttpServletRequest req,HttpServletResponse res,EndElement e,int row,int col,String content) throws IOException,ServletException
		{
	
		}
	
}
